﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace UnrarService
{
    public partial class UnrarService : ServiceBase
    {
        #region Member
        private readonly string _destinationPath;
        private readonly string _watchPath;
        private readonly List<string> _password = new List<string>();
        private readonly DirectoryWatcher _dirWatcher;
        #endregion

        #region Functions/Subs
        //Initialisieren des UnrarService
        public UnrarService()
        {
            //Festlegen des Servicenamens
            SQDebugging.Eventlogging.MyAppName = "UnrarService";

            //Application Config auslesen für die wichtigsten Einstellungen
            _destinationPath = ConfigurationManager.AppSettings["DestinationPath"];
            _watchPath = ConfigurationManager.AppSettings["WatchingPath"];
            string password = ConfigurationManager.AppSettings["Passwords"];

            //Prüfen ob alles erfolgreich ausgelesen werden konnte.
            if (password != null && _destinationPath != null && _watchPath != null)
            {

                foreach (string ePassword in password.Split(';'))
                {
                    _password.Add(ePassword.Trim());
                }

                //Den Directory Watcher initialisieren
                _dirWatcher = new DirectoryWatcher(_watchPath, _password);

                //Loggingausgabe generieren für das Windows Eventlog
                SQDebugging.Eventlogging.WriteErrorLog(String.Format("Dienst lauscht unter: {0}", _watchPath), EventLogEntryType.Information);
            }

            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (_dirWatcher != null)
            {
                _dirWatcher.StartWatcher(_destinationPath);

                //Loggingausgabe generieren für das Windows Eventlog
                SQDebugging.Eventlogging.WriteErrorLog(String.Format("Dienst entpackt nach: {0}", _destinationPath), EventLogEntryType.Information);
            }
        }

        protected override void OnStop()
        {
            
        }
        #endregion
    }
}
